﻿Public Class DetalleRecetaDAO
    '@Accion as int,
    '@DetalleRecetaId as int,
    '@DetalleRecetaPrecioHistorico as float,
    '@DetalleRecetaPorcentajeResolucion as float,
    '@DetalleRecetaPorcentajeCobertura as float,
    '@MedicamentoId as int

    Public Shared Function insertarDetalleReceta(ByVal DetalleRecetaPrecioHistorico As Double, ByVal DetalleRecetaPorcentajeResolucion As Double, ByVal DetalleRecetaPorcentajeCobertura As Double, ByVal DetalleRecetaCantidad As Integer, ByVal DetalleRecetaRenglon As Integer, ByVal MedicamentoId As Integer, ByVal RecetaId As Integer) As Integer
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_detalleReceta"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@DetalleRecetaId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@DetalleRecetaPrecioHistorico", DetalleRecetaPrecioHistorico).DbType = DbType.Double
                    .AddWithValue("@DetalleRecetaPorcentajeResolucion", DetalleRecetaPorcentajeResolucion).DbType = DbType.Double
                    .AddWithValue("@DetalleRecetaPorcentajeCobertura", DetalleRecetaPorcentajeCobertura).DbType = DbType.Double
                    .AddWithValue("@DetalleRecetaCantidad", DetalleRecetaCantidad).DbType = DbType.Int32
                    .AddWithValue("@DetalleRecetaRenglon", DetalleRecetaRenglon).DbType = DbType.Int32
                    .AddWithValue("@MedicamentoId", MedicamentoId).DbType = DbType.Int32
                    .AddWithValue("@RecetaId", RecetaId).DbType = DbType.Int32
                End With
                .Connection = cn
                Return .ExecuteScalar
            End With
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return -1
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarDetalleReceta(ByVal DetalleRecetaId As Integer, ByVal DetalleRecetaPrecioHistorico As Double, ByVal DetalleRecetaPorcentajeResolucion As Double, ByVal DetalleRecetaPorcentajeCobertura As Double, ByVal DetalleRecetaCantidad As Integer, ByVal DetalleRecetaRenglon As Integer, ByVal MedicamentoId As Integer, ByVal RecetaId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_detalleReceta"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@DetalleRecetaId", DetalleRecetaId).DbType = DbType.Int32
                    .AddWithValue("@DetalleRecetaPrecioHistorico", DetalleRecetaPrecioHistorico).DbType = DbType.Double
                    .AddWithValue("@DetalleRecetaPorcentajeResolucion", DetalleRecetaPorcentajeResolucion).DbType = DbType.Double
                    .AddWithValue("@DetalleRecetaPorcentajeCobertura", DetalleRecetaPorcentajeCobertura).DbType = DbType.Double
                    .AddWithValue("@DetalleRecetaCantidad", DetalleRecetaCantidad).DbType = DbType.Int32
                    .AddWithValue("@DetalleRecetaRenglon", DetalleRecetaRenglon).DbType = DbType.Int32
                    .AddWithValue("@MedicamentoId", MedicamentoId).DbType = DbType.Int32
                    .AddWithValue("@RecetaId", RecetaId).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarDetalleReceta(ByVal DetalleRecetaId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_detalleReceta"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@DetalleRecetaId", DetalleRecetaId).DbType = DbType.Int32
                    .AddWithValue("@DetalleRecetaPrecioHistorico", Constante.MENOSUNO).DbType = DbType.Double
                    .AddWithValue("@DetalleRecetaPorcentajeResolucion", Constante.MENOSUNO).DbType = DbType.Double
                    .AddWithValue("@DetalleRecetaPorcentajeCobertura", Constante.MENOSUNO).DbType = DbType.Double
                    .AddWithValue("@DetalleRecetaCantidad", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@DetalleRecetaRenglon", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@MedicamentoId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@RecetaId", Constante.MENOSUNO).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerDetalleReceta(ByVal DetalleRecetaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_detalleReceta_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@DetalleRecetaId", DetalleRecetaId).DbType = DbType.Int32
            da.Fill(ds, "DetalleReceta")

            Return ds.Tables("DetalleReceta")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerDetalleRecetaXReceta(ByVal RecetaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_detalleReceta_obtenerXReceta", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@RecetaId", RecetaId).DbType = DbType.Int32
            da.Fill(ds, "DetalleReceta")

            Return ds.Tables("DetalleReceta")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

End Class

